﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manageweb_TransferAccount_TransferToAB2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"] == null)
        {
            Response.Redirect("~/MemberLogin.aspx");
        }

        if(!Page.IsPostBack)
        {
            GetAccountInfo();
            BindTransferRecords();
        }
    }

    /// <summary>
    /// 获取所有的AB1AB2转账信息
    /// </summary>
    protected void BindTransferRecords()
    {
        SQLExpressDAL.TransferRecordsTableAdapters.TransferRecordsTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.TransferRecordsTableAdapter();
       this.GV_TransferRecords.DataSource=adp.GetTransferRecordByUserID(Session["UserID"].ToString(),"AB1AB2");
       this.GV_TransferRecords.DataBind();
    }

    /// <summary>
    /// 初始化会员的总账户信息
    /// </summary>
    protected void GetAccountInfo()
    {
        SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
        SQLExpressDAL.TransferRecords.UsersAccountRow row = (SQLExpressDAL.TransferRecords.UsersAccountRow)adp.GetSingleAccount(Session["UserID"].ToString()).Rows[0];

        SQLExpressDAL.Users.UsersDataTable tab = (SQLExpressDAL.Users.UsersDataTable)Session["UserInfo"];
        this.lblAB1.Text = row["AB1"].ToString();

        Label lblAB1 = Master.FindControl("lblAB1") as Label;

        Label lblAB2 = Master.FindControl("lblAB2") as Label;

        Label lblAB3 = Master.FindControl("lblAB3") as Label;

        Label lblAB4 = Master.FindControl("lblAB4") as Label;

        lblAB1.Text = row.AB1.ToString("f2");
        lblAB2.Text = row.AB2.ToString("f2");
        lblAB3.Text = row.AB3.ToString("f2");
        lblAB4.Text = row.AB4.ToString("f2");

        this.txtUserAccount.Text = tab.Rows[0]["UserID"].ToString();
        this.txtUserName.Text = tab.Rows[0]["UserName"].ToString();
    }

    /// <summary>
    /// 转账操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        string msg = "转账成功!";
        try
        {           
            string userID = this.txtUserAccount.Text.Trim();
            string userName = this.txtUserName.Text.Trim();
            decimal money = Convert.ToDecimal(this.txtVCurrency.Text.Trim());
            string memo = this.txtMemo.Text.Trim();

            //验证转账金额是否过大
            if (!CheckTransferMoney(money))
            {
                return;
            }

            SQLExpressDAL.DealRecord obj=new SQLExpressDAL.DealRecord();
            int flag = obj.AddTransferMoneyRecord(userID, money, "AB1AB2", "AB1", "AB2", memo);

            if (flag==1)
            {
              msg="转账失败!";
            }

        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }

        Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", string.Format("alert('{0}')",msg), true);
        
        GetAccountInfo();
        BindTransferRecords();
        ClearTxt();

    }
    /// <summary>
    /// 验证转账金额是否超过账户金额
    /// </summary>
    /// <returns></returns>
    protected bool CheckTransferMoney(decimal money)
    {
        bool flag = true;

        try
        {
            decimal currAB1Money =  Convert.ToDecimal(this.lblAB1.Text.Trim());
            decimal transferMoney = money;

            if(transferMoney>currAB1Money)
            {
                flag = false;
            }
        }
        catch (Exception ex)
        {
            flag = false;
        }

        if(!flag)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", string.Format("alert('{0}')", "账户余额不足!"), true);
        }

        return flag;
    }

    protected void GV_TransferRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_TransferRecords.PageIndex = e.NewPageIndex;
        BindRecord();
    }

    protected void ClearTxt()
    {
        this.txtVCurrency.Text = "";
        this.txtMemo.Text = "";
    }
    private decimal totalTransferMoney = 0.0M;

    protected void GV_TransferRecords_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            totalTransferMoney += Convert.ToDecimal(e.Row.Cells[2].Text);            
        }
        else if(e.Row.RowType==DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = totalTransferMoney.ToString();
            e.Row.Cells[2].Style.Add(HtmlTextWriterStyle.TextAlign, "right");            
        }        
    }

    protected void BindRecord()
    {
        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        this.GV_TransferRecords.DataSource = obj.GetMemAccountTransferRecord(Session["UserID"].ToString(), this.txtBeginDate.Text.Trim(), this.txtEndDate.Text.Trim(), "AB1AB2");
        this.GV_TransferRecords.DataBind();
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindRecord();

    }
}
